1 00:00:00,600 --> 00:00:01,470 Welcome back. 2 00:00:01,500 --> 00:00:07,710 This lecture is going to go over all the mathematical operators available in Lua as well as the math 3 00:00:07,710 --> 00:00:08,700 library. 4 00:00:08,730 --> 00:00:14,940 You should already know the basics of math such as adding, subtracting, dividing, multiplying and 5 00:00:14,940 --> 00:00:16,660 exponentiating numbers. 6 00:00:16,680 --> 00:00:22,680 Here you can see all of the different operators responsible for those actions, such as adding, subtracting, 7 00:00:22,680 --> 00:00:26,160 dividing, multiplying, exponentiating. 8 00:00:26,160 --> 00:00:31,620 And then we also know about the modulus operator, which calculates how many times a number can fit 9 00:00:31,620 --> 00:00:34,080 into another number and returns the remainder. 10 00:00:34,110 --> 00:00:35,580 So for example. 11 00:00:36,500 --> 00:00:43,220 If I were to divide seven by three using the modulus operator, we know that we can fit three into seven 12 00:00:43,220 --> 00:00:47,590 two times, and the remainder left over would be the value of one. 13 00:00:47,600 --> 00:00:51,230 Same thing if we did six modules two. 14 00:00:51,260 --> 00:00:54,020 We know that two can fit into six three times. 15 00:00:54,020 --> 00:00:57,620 And because there's not going to be any remainder, the leftover is zero. 16 00:00:57,620 --> 00:01:00,200 And that's what's returned by our modulus operator. 17 00:01:00,620 --> 00:01:04,430 We also have these other operators here that are specific to luoyue. 18 00:01:04,430 --> 00:01:09,200 And the one that we saw previously was this one where we can add a number to itself. 19 00:01:09,200 --> 00:01:14,720 So if we had a variable that stored the value of one instead of doing, let's say a is equal to a plus 20 00:01:14,720 --> 00:01:19,580 one, we can just do a plus equal one, and that achieves the same effect. 21 00:01:20,260 --> 00:01:23,890 We can also do the same thing with subtracting a number by itself. 22 00:01:23,890 --> 00:01:29,590 We can do the same thing with division, multiplication, exponents, and even the modulus operator. 23 00:01:30,310 --> 00:01:33,680 Let's go ahead and take a look at these mathematical operators in action. 24 00:01:33,690 --> 00:01:38,330 I'm going to create a variable called a and just store the value of two. 25 00:01:38,340 --> 00:01:41,250 And then what I'm going to do is put a bunch of print statements here. 26 00:01:41,250 --> 00:01:44,430 And I'm just going to print something like a plus one. 27 00:01:44,640 --> 00:01:47,580 And then I'm going to print out a minus one. 28 00:01:47,610 --> 00:01:50,700 We can print out a divided by two. 29 00:01:50,790 --> 00:01:54,210 We can print out a multiplied by two. 30 00:01:54,710 --> 00:02:03,140 We can print out, let's say, a to the power of four, and then we can print out a modulus to. 31 00:02:03,760 --> 00:02:05,140 What do you think's going to print out? 32 00:02:05,170 --> 00:02:07,120 Well, let's find out if we hit run. 33 00:02:07,990 --> 00:02:10,390 As you can see, a plus one is equal to three. 34 00:02:10,420 --> 00:02:12,940 A minus one is equal to one. 35 00:02:13,120 --> 00:02:15,340 A divided by two is equal to one. 36 00:02:15,640 --> 00:02:22,600 A times two is equal to four, a to the power of four is 16, and then two fits into two one times, 37 00:02:22,600 --> 00:02:27,340 so there's no remainder left over, which means we return or print out the value of zero here. 38 00:02:28,460 --> 00:02:34,190 Now, just like regular mathematics, the interpreter follows the order of operations, which is usually 39 00:02:34,190 --> 00:02:40,430 referred to as Pemdas or Bodmas, which either stands for parentheses, exponents, multiplication, 40 00:02:40,430 --> 00:02:47,720 division, and then addition or subtraction, and bodmas stands for brackets, orders, Division, multiplication, 41 00:02:47,720 --> 00:02:49,180 addition and subtraction. 42 00:02:49,190 --> 00:02:51,500 Both of these acronyms mean the same thing. 43 00:02:51,620 --> 00:02:57,500 So the first things that get operated on are the things that are inside of parentheses, and then the 44 00:02:57,500 --> 00:03:00,380 next are anything that have exponents. 45 00:03:00,560 --> 00:03:04,670 And then after those is going to be multiplication and division. 46 00:03:04,940 --> 00:03:10,070 And then finally the last ones that get operated on are addition and subtraction. 47 00:03:11,130 --> 00:03:12,870 To demonstrate the order of operations. 48 00:03:12,870 --> 00:03:15,480 What I'm going to do is print into the console. 49 00:03:15,480 --> 00:03:20,670 We're going to take a and we're going to wrap our a value inside of parentheses. 50 00:03:20,670 --> 00:03:24,720 Inside of these parentheses I want to add the value of let's say three. 51 00:03:25,500 --> 00:03:28,920 And then I want to raise this to the power of two. 52 00:03:29,190 --> 00:03:34,230 And then afterwards we can subtract this by, let's say, nine divided by three. 53 00:03:34,950 --> 00:03:39,270 Now if the interpreter follows the order of operations, then the value that should get printed into 54 00:03:39,270 --> 00:03:42,150 the console should be the value of 22. 55 00:03:42,270 --> 00:03:44,280 So we can go ahead and hit run. 56 00:03:46,070 --> 00:03:46,790 And there we go. 57 00:03:46,790 --> 00:03:52,910 We get the value of 22 as we expect, proving that the interpreter follows the order of operations. 58 00:03:53,600 --> 00:03:57,950 Now, the last thing I want to talk about here is going to be the math library. 59 00:03:58,250 --> 00:04:03,980 And this gives us access to a bunch of functions that aren't implemented as operators, such as getting 60 00:04:03,980 --> 00:04:10,370 the absolute value of a number, the square root of a number, and many other trigonometric functions. 61 00:04:10,400 --> 00:04:14,600 Now, I'm not going to be your math professor today, so I'm not going to go into much depth about all 62 00:04:14,600 --> 00:04:19,250 of these mathematical functions, as that would take way too long and you probably won't remember any 63 00:04:19,250 --> 00:04:19,670 of it. 64 00:04:19,700 --> 00:04:25,490 However, the math library also has some functions that give us, for example, the smallest number 65 00:04:25,490 --> 00:04:30,560 that is greater than or equal to a number we specify, which is useful for floats. 66 00:04:30,590 --> 00:04:37,250 We have other functions to get the largest number that is smaller than or equal to a number we specify. 67 00:04:37,250 --> 00:04:41,990 We can use some other functions which are called math.max. 68 00:04:43,930 --> 00:04:48,110 Which will return the largest number out of the numbers we pass to the function. 69 00:04:48,130 --> 00:04:53,650 We also have math.min, which will return the smallest number out of the numbers we pass to the function, 70 00:04:53,650 --> 00:04:55,480 and there's a whole bunch of other ones. 71 00:04:55,990 --> 00:04:59,830 So we go ahead and index the math library by typing out math. 72 00:04:59,830 --> 00:05:04,600 As you can see, there's just a whole bunch of different mathematical functions, trigonometric functions, 73 00:05:04,600 --> 00:05:07,630 and just a bunch of stuff that would take way too long to learn about. 74 00:05:07,630 --> 00:05:10,810 But they're all here inside of the math library for us to use. 75 00:05:10,840 --> 00:05:16,510 Specifically here, I want to focus on the math dot random function that we took a look at earlier, 76 00:05:16,510 --> 00:05:21,760 and this function returns by default a pseudo random number between 0 and 1. 77 00:05:21,760 --> 00:05:28,480 When we do not specify a range or pass a range to the function, they're called pseudo random numbers, 78 00:05:28,480 --> 00:05:34,510 because numbers generated by a computer are never truly random, and this is because computers are awful 79 00:05:34,510 --> 00:05:36,400 at generating random numbers. 80 00:05:36,550 --> 00:05:43,600 Now we can also specify a seed for the random function, which means that the number that gets returned 81 00:05:43,600 --> 00:05:46,300 by this function is based on the seed. 82 00:05:46,330 --> 00:05:52,780 A seed will always return the same number, just like how a seed in a video game like Minecraft will 83 00:05:52,780 --> 00:05:54,760 always return the same world. 84 00:05:55,440 --> 00:05:59,400 To demonstrate this, I'm going to create a for loop that loops five times. 85 00:05:59,400 --> 00:06:01,710 So we can do for I is equal to one. 86 00:06:01,710 --> 00:06:04,260 And we want to loop five times. 87 00:06:04,380 --> 00:06:11,460 And what we want to do is we want to set the math dot random seed equal to the current index. 88 00:06:11,460 --> 00:06:16,470 And then we want to print out what gets returned by our math dot random function. 89 00:06:17,720 --> 00:06:23,210 Now, since this math notrillionandom function is going to return a value based on the seed, every 90 00:06:23,210 --> 00:06:28,580 single time we execute this loop, we should expect the same number to get printed out through each 91 00:06:28,610 --> 00:06:30,300 iteration of this loop. 92 00:06:30,320 --> 00:06:37,100 So with a random seed of one, any time we execute this math dot random function, as long as it has 93 00:06:37,100 --> 00:06:40,890 a seed of one, it's always going to return the same number. 94 00:06:40,910 --> 00:06:42,800 So if we run our game here. 95 00:06:44,020 --> 00:06:49,180 As you can see, we get five different numbers based on those seeds from 1 to 5. 96 00:06:49,210 --> 00:06:54,070 And then if we go ahead and stop the game and then run our game again. 97 00:06:54,250 --> 00:06:59,200 As you can see, we get the exact same numbers printed out in the console, and we can keep stopping 98 00:06:59,200 --> 00:07:04,420 the game and rerunning the game over and over again, because these will always print the same numbers 99 00:07:04,420 --> 00:07:04,750 out. 100 00:07:04,750 --> 00:07:08,860 And that's because those numbers are based on the seed that we pass here. 101 00:07:09,420 --> 00:07:13,590 If we do not want to specify a seed, then we can just go ahead and delete this. 102 00:07:13,590 --> 00:07:17,790 And our Math.random function will continue to generate random numbers inside of the console. 103 00:07:17,790 --> 00:07:19,110 So if we hit run. 104 00:07:19,990 --> 00:07:25,420 As you can see, we get a bunch of random numbers between 0 and 1, and then if we hit stop and then 105 00:07:25,420 --> 00:07:26,860 run the test again. 106 00:07:27,490 --> 00:07:30,420 Here we get a complete different set of random numbers. 107 00:07:30,430 --> 00:07:33,580 And one last time, if we hit stop and run again. 108 00:07:34,700 --> 00:07:37,670 We get another set of random numbers. 109 00:07:38,720 --> 00:07:39,290 All righty. 110 00:07:39,290 --> 00:07:41,300 That is all from me for this lecture. 111 00:07:41,300 --> 00:07:43,790 I hope you enjoyed and I will see you in the next one. 112 00:07:43,820 --> 00:07:44,930 Happy scripting.